/*
 * The basic class for creating animated objects, alongside the canvas class. Designed
 * to be extended for various objects.
 */

package game;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.*;

import javax.swing.ImageIcon;

public abstract class Sprite extends GameRectangle{
	public static final long serialVersionUID = 1L;
	protected ImageIcon[] frames;  //various frames of the sprite
	protected int[][] animations;  //array of integer arrays to store image addresses for specific animations
	protected int currentAnim; //current animation
	protected int currentFrame; //current frame of the current animation
	protected int slowDown; //amount of slowdown for animation
	protected int speedIndex; //current index for slowing down animation
	protected int speed;  //speed of the sprite
	protected float scale; //scale of the sprite
	protected Rectangle moveBounds;
	//effected physic values
	public int fxSpeed;
	public int fxJumpHeight;
	public float fxGravity;
	public float fxFriction;
	public boolean looped;
	public int curWidth;
	public int curHeight;
	
	@Override
	public void update(){
		super.update();
	}
	@Override
	public abstract void calculateIntersectEffects(GameRectangle rect);
	
	public Sprite(){
		super();
		currentAnim = 0;
		currentFrame = 0;
		slowDown = 1;
		speedIndex = 1;
		scale = 1;
		height = 0;
		width = 0;
		looped = false;
		curWidth = width;
		curHeight = height;
	}
	
	public Sprite(Level g, ImageIcon[] i){
		super(g, 0, 0, i[0].getIconWidth(), i[0].getIconHeight());
		frames = i;
		currentAnim = 0;
		currentFrame = 0;
		slowDown = 1;
		speedIndex = 1;
		scale = 1;
		animations = new int[1][1];
		height = i[0].getIconHeight();
		width = i[0].getIconWidth();
		looped = false;
		curWidth = width;
		curHeight = height;
	}
	
	public Sprite(Level g, ImageIcon i){
		super(g, 0, 0, i.getIconWidth(), i.getIconHeight());
		frames = new ImageIcon[1];
		frames[0] = i;
		currentAnim = 0;
		currentFrame = 0;
		slowDown = 1;
		speedIndex = 1;
		scale = 1;
		animations = new int[1][1];
		height = i.getIconHeight();
		width = i.getIconWidth();
		looped = false;
		curWidth = width;
		curHeight = height;
	}
	
	public Sprite(Level g, ImageIcon i, int x, int y, int s){
		super(g, x, y, i.getIconWidth(), i.getIconHeight());
		frames = new ImageIcon[1];
		frames[0] = i;
		scale = s;
		currentAnim = 0;
		currentFrame = 0;
		slowDown = 1;
		speedIndex = 1;
		animations = new int[1][1];
		looped = false;
		curWidth = width*s;
		curHeight = height*s;
	}
	
	public Sprite(Level g, ImageIcon i, int x, int y, int slow, int s){
		super(g, x, y, i.getIconWidth(), i.getIconHeight());
		frames = new ImageIcon[1];
		frames[0] = i;
		scale = s;
		currentAnim = 0;
		currentFrame = 0;
		slowDown = slow;
		speedIndex = 1;
		animations = new int[1][1];
		looped = false;
		curWidth = width*s;
		curHeight = height*s;
	}
	
	public Sprite(Level g, ImageIcon[] i, int x, int y){
		super(g, x, y, i[0].getIconWidth(), i[0].getIconHeight());
		frames = i;
		currentAnim = 0;
		currentFrame = 0;
		slowDown = 1;
		speedIndex = 1;
		animations = new int[1][1];
		looped = false;
		curWidth = width;
		curHeight = height;
	}
	
	public Sprite(Level g, ImageIcon[] i, int x, int y, int slow){
		super(g, x, y, i[0].getIconWidth(), i[0].getIconHeight());
		frames = i;
		currentAnim = 0;
		currentFrame = 0;
		slowDown = slow;
		speedIndex = 1;
		animations = new int[1][1];
		looped = false;
		curWidth = width;
		curHeight = height;
	}
	
	public void setAnim(int anim){
		currentAnim = anim;
		currentFrame = 0;
	}
	
	public void setSlowDown(int slow){
		slowDown = slow;
	}
	
	public void nextFrame(){
		if(speedIndex>=slowDown){
			if(currentFrame < animations[currentAnim].length -1){
				currentFrame++;
				looped = false;
			}else{
				currentFrame = 0;
				looped = true;
			}
			speedIndex = 1;
		}else{
			speedIndex++;
			looped = false;
		}
		width = getCurrentFrame().getWidth(level.canvas);
		height = getCurrentFrame().getHeight(level.canvas);
		curWidth = (int)(width*scale);
		curHeight = (int)(height*scale);
	}
	
	public void prevFrame(){
		if(currentFrame > 0){
			currentFrame--;
		}
	}
	
	public Image getCurrentFrame(){
		if(frames != null){
			return frames[animations[currentAnim][currentFrame]].getImage();
		}
		return null;
	}
	
	public int getAnim(){
		return currentAnim;
	}
	
	public int getFrameIndex(){
		return currentFrame;
	}
	
	public int width(){
		return width;
	}
	
	public int height(){
		return height;
	}
	
	@Override
	public void draw(){
		level.canvas.bufGraphics.drawImage(getCurrentFrame(), x-level.canvas.currentX, y-level.canvas.currentY, curWidth, curHeight, level.canvas);
	}
	
	public void rightHit(GameRectangle g){
		if(!(xMotion() < g.xMotion())){
			x = g.x-curWidth;
			xMotion = g.xMotion();
		}
	}
	
	public void leftHit(GameRectangle g){
		if(!(xMotion() > g.xMotion())){
			x = g.x+g.width;
			xMotion = g.xMotion();
		}
	}
	
	public void topHit(GameRectangle g){
		if(!(downMotion() > g.downMotion())){
			y = g.y+g.height;
			downMotion = g.downMotion();
		}
	}
	
	public void bottomHit(GameRectangle g){
		if(!(downMotion() < g.downMotion())){
			y = g.y-curHeight;
			downMotion = g.downMotion();
		}
	}
}
